﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using SayDemo.Data;
using SayDemo.Models;
using SayDemo.ViewModel;

namespace SayDemo.Controllers
{
    public class MessagesController : Controller
    {
        private SayDemoDb db = new SayDemoDb();

        // GET: Messages
        public ActionResult Index()
        {
            if (Session["UserName"] == null)
            {
                return View("~/Views/users/login.cshtml");
            }
            //var id = 1;

            //var currentUser = db.Users.Where(x => x.Id == id).SingleOrDefault();

            // 获取所有的用户
            var userList = db.Users.ToList();
            //获取所有说说
            var msgList = db.Messages.ToList();
            //获取所有评论
            var comList = db.Comments.ToList();

            //存放消息
            var msgViewModelList = new List<MessageViewModel>();
            //var comViewModelList = new List<CommentViewModel>();

            foreach (var msg in msgList)
            {
                var tmpComList = new List<CommentViewModel>();
                var currentMsgComments = comList.Where(x => x.MsgId == msg.Id).ToList();

                foreach (var c in currentMsgComments)
                {
                    tmpComList.Add(new CommentViewModel
                    {
                        FromUserName = userList.SingleOrDefault(x => x.Id == c.FromUserId).Username,
                        ToUserName=userList.SingleOrDefault(x=>x.Id==msgList.SingleOrDefault(t => t.Id == c.MsgId).FromUserId).Username,
                        Comment=c.Content
                    });
                }

                msgViewModelList.Add(new MessageViewModel
                {
                    Id = msg.Id,
                    FromUserId = msg.FromUserId,
                    Content = msg.Content,
                    Comments = tmpComList
                });
            }

            var data = new SayViewModel
            {
                Username = Session["UserName"].ToString(),
                //Username=currentUser.Username,
                Messages =msgViewModelList

            };

            return View(data);
        }

        // GET: Messages/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Messages messages = db.Messages.Find(id);
            if (messages == null)
            {
                return HttpNotFound();
            }
            return View(messages);
        }

        // GET: Messages/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Messages/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,FromUserId,Content,CreatedAt,UpdatedAt,Version,Remarks")] Messages messages)
        {
            if (ModelState.IsValid)
            {
                db.Messages.Add(messages);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(messages);
        }

        // GET: Messages/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Messages messages = db.Messages.Find(id);
            if (messages == null)
            {
                return HttpNotFound();
            }
            return View(messages);
        }

        // POST: Messages/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,FromUserId,Content,CreatedAt,UpdatedAt,Version,Remarks")] Messages messages)
        {
            if (ModelState.IsValid)
            {
                db.Entry(messages).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(messages);
        }

        // GET: Messages/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Messages messages = db.Messages.Find(id);
            if (messages == null)
            {
                return HttpNotFound();
            }
            return View(messages);
        }

        // POST: Messages/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Messages messages = db.Messages.Find(id);
            db.Messages.Remove(messages);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
